package com.cherry.request.request

import android.content.Context
import com.cherry.request.cache.CacheMode
import okhttp3.*
import okhttp3.internal.http.HttpHeaders
import org.apache.http.params.HttpParams
import retrofit2.CallAdapter
import retrofit2.Converter
import retrofit2.Retrofit
import java.net.Proxy
import java.util.*
import java.util.concurrent.TimeUnit
import javax.net.ssl.HostnameVerifier

/**
 * @author 董棉生(dongmiansheng@parkingwang.com)
 * @since 18-12-25
 */

abstract class BaseRequest {

    protected var cache: Cache? = null
    protected var cacheMode = CacheMode.NO_CACHE                    //默认无缓存
    protected var cacheTime: Long = -1                                         //缓存时间
    protected var cacheKey: String? = null                                             //缓存Key
    protected var diskConverter: IDiskConverter? = null                                //设置Rxcache磁盘转换器
    protected var baseUrl: String? = null                                              //BaseUrl
    protected var url: String? = ""                                                  //请求url
    protected var readTimeOut: Long = 0                                            //读超时
    protected var writeTimeOut: Long = 0                                           //写超时
    protected var connectTimeout: Long = 0                                         //链接超时
    protected var retryCount: Int = 0                                              //重试次数默认3次
    protected var retryDelay: Int = 0                                              //延迟xxms重试
    protected var retryIncreaseDelay: Int = 0                                      //叠加延迟
    protected var isSyncRequest: Boolean = false                                       //是否是同步请求
    protected var cookies: MutableList<Cookie> = ArrayList()                    //用户手动添加的Cookie
    protected val networkInterceptors: MutableList<Interceptor> = ArrayList()
    protected var headers = HttpHeaders()                     //添加的header
    var params: HttpParmas? = null
    protected var retrofit: Retrofit
    //    protected var rxCache: RxCache                                             //rxcache缓存
//    protected var apiManager: ApiService                                       //通用的的api接口
    protected var okHttpClient: OkHttpClient
    protected var context: Context
    private var sign = false                                          //是否需要签名
    private var timeStamp = false                                     //是否需要追加时间戳
    private var accessToken = false                                   //是否需要追加token
    protected var httpUrl: HttpUrl? = null
    protected var proxy: Proxy? = null
    //    protected var sslParams: HttpsUtils.SSLParams? = null
    protected var hostnameVerifier: HostnameVerifier? = null
    protected var converterFactories: MutableList<Converter.Factory> = ArrayList()
    protected var adapterFactories: MutableList<CallAdapter.Factory> = ArrayList()
    protected val interceptors: MutableList<Interceptor> = ArrayList()


    private fun getOkHttpClient(): OkHttpClient.Builder {
        if (readTimeOut <= 0 && writeTimeOut <= 0 && connectTimeout <= 0 && sslParams == null
                && cookies.size == 0 && hostnameVerifier == null && proxy == null && headers.isEmpty()) {
            val builder = EasyHttp.getOkHttpClientBuilder()
            for (interceptor in builder.interceptors()) {
                if (interceptor is BaseDynamicInterceptor) {
                    (interceptor as BaseDynamicInterceptor).sign(sign).timeStamp(timeStamp).accessToken(accessToken)
                }
            }
            return builder
        } else {
            val newClientBuilder = EasyHttp.getOkHttpClient().newBuilder()
            if (readTimeOut > 0)
                newClientBuilder.readTimeout(readTimeOut, TimeUnit.MILLISECONDS)
            if (writeTimeOut > 0)
                newClientBuilder.writeTimeout(writeTimeOut, TimeUnit.MILLISECONDS)
            if (connectTimeout > 0)
                newClientBuilder.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
            if (hostnameVerifier != null) newClientBuilder.hostnameVerifier(hostnameVerifier)
            if (sslParams != null)
                newClientBuilder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
            if (proxy != null) newClientBuilder.proxy(proxy)
            if (cookies.size > 0) EasyHttp.getCookieJar().addCookies(cookies)

            //添加头  头添加放在最前面方便其他拦截器可能会用到
            newClientBuilder.addInterceptor(HeadersInterceptor(headers))

            for (interceptor in interceptors) {
                if (interceptor is BaseDynamicInterceptor) {
                    (interceptor as BaseDynamicInterceptor).sign(sign).timeStamp(timeStamp).accessToken(accessToken)
                }
                newClientBuilder.addInterceptor(interceptor)
            }
            for (interceptor in newClientBuilder.interceptors()) {
                if (interceptor is BaseDynamicInterceptor) {
                    (interceptor as BaseDynamicInterceptor).sign(sign).timeStamp(timeStamp).accessToken(accessToken)
                }
            }
            if (networkInterceptors.size > 0) {
                for (interceptor in networkInterceptors) {
                    newClientBuilder.addNetworkInterceptor(interceptor)
                }
            }
            return newClientBuilder
        }

        protected fun build() {

        }

    }